<?php
// +----------------------------------------------------------------------
// | Fanwe 方维众筹商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 甘味人生(526130@qq.com)
// +----------------------------------------------------------------------

class BmAnchorPropAction extends CommonAction
{
    public function index()
    {
		$bm_qrcode_id=intval($_REQUEST['bm_qrcode_id']);
        $to_user_id=intval($_REQUEST['to_user_id']);
        $login_name=strim($_REQUEST['login_name']);
        $login_name1=strim($_REQUEST['login_name1']);
        $time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}
        //表不存跳出
        $table=DB_PREFIX."video_prop_".$time['begin_time_ym'];
        if(!mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))) {

            $this->assign('begin_time', to_date($time['begin_time'], 'Y-m-d'));
            $this->assign('end_time', to_date($time['end_time'], 'Y-m-d'));
            $this->assign('to_user_id', $to_user_id);
            $this->assign('bm_qrcode_id', $bm_qrcode_id);
            $this->assign('login_name', $login_name);

            $this->display(); exit;
        }
		//表不存跳出
        $table=DB_PREFIX."video_prop_".$time['begin_time_ym'];
        if(!mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))) {
            
			$this->assign('begin_time', to_date($time['begin_time'], 'Y-m-d'));
			$this->assign('end_time', to_date($time['end_time'], 'Y-m-d'));
			$this->assign('to_user_id', $to_user_id);
			$this->assign('bm_qrcode_id', $bm_qrcode_id);
			$this->assign('login_name', $login_name);
            
			$this->display(); exit;
        }
		
		$where = " l.create_time between " . $time['begin_time'] . " and " . $time['end_time'] . " and u.is_authentication=2 and p.member_id =0 and p.type=2 and l.check_type = 0 ";
        if($bm_qrcode_id != ""){
            $where.=" and u.bm_qrcode_id = ".$bm_qrcode_id."";
        }
        if($to_user_id >0){ 
            $where.=" and l.to_user_id = ".$to_user_id."";
        }
		if($login_name){
     		$where .=" and p.login_name = '".$login_name."' ";
     	}elseif ($login_name1){
     		$where .= " and p.login_name like '%" . $login_name1 . "%' ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "l.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
					
		$sql_count="select 
		 				l.id,
						l.to_user_id,
						sum(l.total_diamonds) as total_diamonds,
						sum(l.total_coins) as total_coins,
						sum(l.total_ticket) as total_ticket,
						l.create_time,
						u.nick_name,
						u.bm_pid,
						u.bm_qrcode_id,
						u.bm_promoter_id as qrcode_promoter_id,
						p.login_name as p_login_name 
					from ".$pre."video_prop_".$time['begin_time_ym']." as l
					left join ".$pre."user as u on u.id = l.to_user_id 
					left join ".$pre."bm_promoter as p on p.user_id = u.bm_pid 
					where 
						".$where." 
					GROUP BY l.to_user_id";

					
		$list_count_all=$GLOBALS['db']->getAll($sql_count);
		
		$p = $_REQUEST['p'];
		if ($p == '') {
			$p = 1;
		}
		$p         = $p > 0 ? $p : 1;
		$page_size = 10;
		$limit     = (($p - 1) * $page_size) . "," . $page_size;
		
        $count=count($list_count_all);

        if($count > 0){

			$page      = new Page($count, $page_size);
			$page_show = $page->show();
	
			$sql="select 
					l.id,
					l.to_user_id,
					sum(l.total_diamonds) as total_diamonds,
					sum(l.total_coins) as total_coins,
					sum(l.total_ticket) as total_ticket,
					l.create_time,
					u.nick_name,
					u.bm_pid,
					u.bm_qrcode_id,
					u.bm_promoter_id as qrcode_promoter_id,
					p.login_name as p_login_name 
				from 
					".$pre."video_prop_".$time['begin_time_ym']." as l
					left join ".$pre."user as u on u.id = l.to_user_id 
					left join ".$pre."bm_promoter as p on p.user_id = u.bm_pid 
					where 
						".$where." 
				GROUP BY l.to_user_id 
				order by ".$order." ".$sort." 
				limit ".$limit; 
			
			$list = $GLOBALS['db']->getAll($sql);
			
			$bm_config = load_auto_cache("bm_config");
			//代理商签约主播收益分成
			$promoter_sign_anchor_revenue = intval($bm_config['promoter_sign_anchor_revenue']);
			if ($promoter_sign_anchor_revenue < -1 || $promoter_sign_anchor_revenue > 100) {
				$promoter_sign_anchor_revenue = 90;
			}
			//代理商普通主播收益分成
			$promoter_average_anchor_revenue = intval($bm_config['promoter_average_anchor_revenue']);
			if ($promoter_average_anchor_revenue < -1 || $promoter_average_anchor_revenue > 100) {
				$promoter_average_anchor_revenue = 70;
			}
	
			foreach($list as $k=>$v){
				$qrcode_promoter_ids[$v['qrcode_promoter_id']]=$v['qrcode_promoter_id'];
	
				//计算
				$list[$k]['user_ticket'] = $v['total_ticket'] * ($promoter_average_anchor_revenue / 100);
				$list[$k]['promoter_ticket'] = $list[$k]['total_ticket'] - $list[$k]['user_ticket'];
	
			}
			$qrcode_promoter_ids=array_filter($qrcode_promoter_ids);
			if($qrcode_promoter_ids){
				$qrcode_promoter_names=$GLOBALS['db']->getAll("select id,name,login_name from ".$pre."bm_promoter where id in(".implode(',',$qrcode_promoter_ids).") ");
				foreach($qrcode_promoter_names as $k=>$v){
					$promoter_names[$v['id']]['name']=$v['name'];
					$promoter_names[$v['id']]['login_name']=$v['login_name'];
				}
	
				foreach($list as $k=>$v){
					$list[$k]['qrcode_promoter_name']=$promoter_names[$v['qrcode_promoter_id']]['name'];
					$list[$k]['qrcode_promoter_login_name']=$promoter_names[$v['qrcode_promoter_id']]['login_name'];
				}
			}

			//合计
            $total_re=array();
            foreach($list_count_all as $k =>$v){
                $total_re['total_diamonds'] +=$v['total_diamonds'];
                $total_re['total_coins'] +=$v['total_coins'];
                $total_re['total_ticket'] +=$v['total_ticket'];
            }
            $total_re['user_ticket'] = $total_re['total_ticket'] * ($promoter_average_anchor_revenue / 100);
            $total_re['promoter_ticket'] = $total_re['total_ticket'] - $total_re['user_ticket'];

		}
		
		
		$sortImg = $sort; //排序图标
		$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
		$sort = $sort == 'desc' ? 1 : 0; //排序方式
		//模板赋值显示

		$this->assign ( 'sort', $sort );
		$this->assign ( 'order', $order );
		$this->assign ( 'sortImg', $sortImg );
		$this->assign ( 'sortType', $sortAlt );
		
		
        $this->assign('list', $list);
        $this->assign('page', $page_show);
		$this->assign('begin_time', to_date($time['begin_time'], 'Y-m-d'));
		$this->assign('end_time', to_date($time['end_time'], 'Y-m-d'));
		$this->assign('to_user_id', $to_user_id);
		$this->assign('bm_qrcode_id', $bm_qrcode_id);
		$this->assign('login_name', $login_name);
		$this->assign('total_re', $total_re);
        $this->assign('list', $list);
		
		$m_config = load_auto_cache("m_config");
		$this->assign('ticket_name', $m_config['ticket_name']);
		$bm_config = load_auto_cache("bm_config");
		$this->assign('bm_one',$bm_config['bm_one']);
		$this->assign('bm_two',$bm_config['bm_two']); 
		
        $this->display();
    }
	//导出电子表
    public function export_csv($page = 1)
    {

        $pagesize = 10;
        set_time_limit(0);
        $limit = (($page - 1)*intval($pagesize)).",".(intval($pagesize));
		
		$date = $_REQUEST;
		
		/*********************/
		
		/*------------------------*/
		$bm_qrcode_id=intval($_REQUEST['bm_qrcode_id']);
        $to_user_id=intval($_REQUEST['to_user_id']);
        $login_name=strim($_REQUEST['login_name']);
        $login_name1=strim($_REQUEST['login_name1']);
        $time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}
        //表不存跳出
        $table=DB_PREFIX."video_prop_".$time['begin_time_ym'];
        if(!mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))) {

            $this->assign('begin_time', to_date($time['begin_time'], 'Y-m-d'));
            $this->assign('end_time', to_date($time['end_time'], 'Y-m-d'));
            $this->assign('to_user_id', $to_user_id);
            $this->assign('bm_qrcode_id', $bm_qrcode_id);
            $this->assign('login_name', $login_name);

            $this->display(); exit;
        }
		//表不存跳出
        $table=DB_PREFIX."video_prop_".$time['begin_time_ym'];
        if(!mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))) {
            
			$this->assign('begin_time', to_date($time['begin_time'], 'Y-m-d'));
			$this->assign('end_time', to_date($time['end_time'], 'Y-m-d'));
			$this->assign('to_user_id', $to_user_id);
			$this->assign('bm_qrcode_id', $bm_qrcode_id);
			$this->assign('login_name', $login_name);
            
			$this->display(); exit;
        }
		
		$where = " l.create_time between " . $time['begin_time'] . " and " . $time['end_time'] . " and u.is_authentication=2 and p.member_id =0 and p.type=2 and l.check_type = 0 ";
        if($bm_qrcode_id != ""){
            $where.=" and u.bm_qrcode_id = ".$bm_qrcode_id."";
        }
        if($to_user_id >0){ 
            $where.=" and l.to_user_id = ".$to_user_id."";
        }
		if($login_name){
     		$where .=" and p.login_name = '".$login_name."' ";
     	}elseif ($login_name1){
     		$where .= " and p.login_name like '%" . $login_name1 . "%' ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "l.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
					
		$sql_count="select 
		 				l.id,
						l.to_user_id,
						sum(l.total_diamonds) as total_diamonds,
						sum(l.total_coins) as total_coins,
						sum(l.total_ticket) as total_ticket,
						l.create_time,
						u.nick_name,
						u.bm_pid,
						u.bm_qrcode_id,
						u.bm_promoter_id as qrcode_promoter_id,
						p.login_name as p_login_name 
					from ".$pre."video_prop_".$time['begin_time_ym']." as l
					left join ".$pre."user as u on u.id = l.to_user_id 
					left join ".$pre."bm_promoter as p on p.user_id = u.bm_pid 
					where 
						".$where." 
					GROUP BY l.to_user_id";

					
		$list_count_all=$GLOBALS['db']->getAll($sql_count);
		
        $count=count($list_count_all);

        if($count > 0){
	
			$sql="select 
					l.id,
					l.to_user_id,
					sum(l.total_diamonds) as total_diamonds,
					sum(l.total_coins) as total_coins,
					sum(l.total_ticket) as total_ticket,
					l.create_time,
					u.nick_name,
					u.bm_pid,
					u.bm_qrcode_id,
					u.bm_promoter_id as qrcode_promoter_id,
					p.login_name as p_login_name 
				from 
					".$pre."video_prop_".$time['begin_time_ym']." as l
					left join ".$pre."user as u on u.id = l.to_user_id 
					left join ".$pre."bm_promoter as p on p.user_id = u.bm_pid 
					where 
						".$where." 
				GROUP BY l.to_user_id 
				order by ".$order." ".$sort." 
				limit ".$limit; 
			
			$list = $GLOBALS['db']->getAll($sql);
			
			$bm_config = load_auto_cache("bm_config");
			//代理商签约主播收益分成
			$promoter_sign_anchor_revenue = intval($bm_config['promoter_sign_anchor_revenue']);
			if ($promoter_sign_anchor_revenue < -1 || $promoter_sign_anchor_revenue > 100) {
				$promoter_sign_anchor_revenue = 90;
			}
			//代理商普通主播收益分成
			$promoter_average_anchor_revenue = intval($bm_config['promoter_average_anchor_revenue']);
			if ($promoter_average_anchor_revenue < -1 || $promoter_average_anchor_revenue > 100) {
				$promoter_average_anchor_revenue = 70;
			}
	
			foreach($list as $k=>$v){
				$qrcode_promoter_ids[$v['qrcode_promoter_id']]=$v['qrcode_promoter_id'];
	
				//计算
				$list[$k]['user_ticket'] = $v['total_ticket'] * ($promoter_average_anchor_revenue / 100);
				$list[$k]['promoter_ticket'] = $list[$k]['total_ticket'] - $list[$k]['user_ticket'];
	
			}
			$qrcode_promoter_ids=array_filter($qrcode_promoter_ids);
			if($qrcode_promoter_ids){
				$qrcode_promoter_names=$GLOBALS['db']->getAll("select id,name,login_name from ".$pre."bm_promoter where id in(".implode(',',$qrcode_promoter_ids).") ");
				foreach($qrcode_promoter_names as $k=>$v){
					$promoter_names[$v['id']]['name']=$v['name'];
					$promoter_names[$v['id']]['login_name']=$v['login_name'];
				}
	
				foreach($list as $k=>$v){
					$list[$k]['qrcode_promoter_name']=$promoter_names[$v['qrcode_promoter_id']]['name'];
					$list[$k]['qrcode_promoter_login_name']=$promoter_names[$v['qrcode_promoter_id']]['login_name'];
				}
			}

			//合计
            $total_re=array();
            foreach($list_count_all as $k =>$v){
                $total_re['total_diamonds'] +=$v['total_diamonds'];
                $total_re['total_coins'] +=$v['total_coins'];
                $total_re['total_ticket'] +=$v['total_ticket'];
            }
            $total_re['user_ticket'] = $total_re['total_ticket'] * ($promoter_average_anchor_revenue / 100);
            $total_re['promoter_ticket'] = $total_re['total_ticket'] - $total_re['user_ticket'];

		}
		else
		{
			if($page==1)
				$this->error(L("NO_RESULT"));
		}

		/*------------------------*/
		/*********************/
		$voList = $list;
		
        if($voList)
        {
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
			//模板赋值显示
	
			
			$bm_config = load_auto_cache("bm_config");
			$m_config = load_auto_cache("m_config");
			
            register_shutdown_function(array(&$this, 'export_csv'), $page+1);

            $refund_value = array( 'to_user_id'=>'""', 'nick_name'=>'""', 'total_diamonds'=>'""','total_coins'=>'""','total_ticket'=>'""','user_ticket'=>'""','promoter_ticket'=>'""','p_login_name'=>'""','qrcode_promoter_login_name'=>'""','bm_qrcode_id'=>'""');
            if($page == 1)
            {
                $content = iconv("utf-8","gbk","主播ID,主播名称,收到礼物钻石,收到礼物游戏币,".$m_config['ticket_name']."总留存,主播".$m_config['ticket_name']."留存,".$bm_config['bm_two'].$m_config['ticket_name']."留存,所属".$bm_config['bm_two']."编号,所属代理商编号,所属业务员ID");
                $content = $content . "\n";
            }


            foreach($voList as $k=>$v)
            {
				$refund_value['to_user_id'] = '"' . iconv('utf-8','gbk',$v['to_user_id']) . '"'."\t";
				$refund_value['nick_name'] = '"' . iconv('utf-8','gbk',$v['nick_name']) . '"'."\t";
				$refund_value['total_diamonds'] = '"' . iconv('utf-8','gbk',$v['total_diamonds']) . '"'."\t";
				$refund_value['total_coins'] = '"' . iconv('utf-8','gbk',$v['total_coins']) . '"'."\t";
				$refund_value['total_ticket'] = '"' . iconv('utf-8','gbk',$v['total_ticket']) . '"'."\t";
				$refund_value['user_ticket'] = '"' . iconv('utf-8','gbk',$v['user_ticket']) . '"'."\t";
				$refund_value['promoter_ticket'] = '"' . iconv('utf-8','gbk',$v['promoter_ticket']) . '"'."\t";
				$refund_value['p_login_name'] = '"' . iconv('utf-8','gbk',$v['p_login_name']) . '"'."\t";
				$refund_value['qrcode_promoter_login_name'] = '"' . iconv('utf-8','gbk',$v['qrcode_promoter_login_name']) . '"'."\t";
				$refund_value['bm_qrcode_id'] = '"' . iconv('utf-8','gbk',$v['bm_qrcode_id']) . '"'."\t";
				
                $content .= implode(",", $refund_value) . "\n";
            }

            //
            header("Content-Disposition: attachment; filename=refund_list.csv");

            echo $content ;
        }
        else
        {
            if($page==1)
                $this->error(L("NO_RESULT"));
        }

    }
}
